package com.bj58.ecdata.calc.dbservice.dao;

import java.util.Collections;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Tuple;

import com.bj58.ecdata.calc.dbservice.dao.consts.DaoConst;
import com.bj58.ecdata.calc.dbservice.dao.model.InfoModel;
import com.bj58.ecdata.exception.BusinessException;
import com.bj58.ecdata.redis.JedisX;
import com.bj58.ecdata.util.DateUtils;

@Component
public class InfoDao extends BaseDao<InfoModel>{

	@Autowired(required = false)
	@Qualifier("cachePool_handler")
	protected JedisX cacheHandler;
	
	public Set<Tuple> loadTopCateList(InfoModel model, int count) {
		try {
			Set<Tuple> result = super.cacheLoadsDailyZset("belong_cate2", model, 0, count - 1);
			if (result != null)
				return result;
			else
				return Collections.emptySet();
		} catch (Exception e) {
			throw new BusinessException(e);
		}
	}
	
	public  long getInfoUserCount () {
		try {
			long time = super.getLatest();
			String date = DateUtils.dateFormat(time, "yyyy-MM-dd");
			return cacheHandler.scard(date+DaoConst.INFO_UID_SET);
		} catch (Exception e) {
			throw new BusinessException(e);
		}
	}
	
	
	@Override
	protected String getCreateSQL() {
		 return "CREATE TABLE %s ("
			  +"`time` bigint(20) NOT NULL,"
			  +"`platform` varchar(50) NOT NULL,"
			  +"`bussline` varchar(20) NOT NULL DEFAULT '',"
			  +"`belong_cate1` varchar(50) NOT NULL DEFAULT '',"
			  +"`belong_cate2` varchar(50) NOT NULL DEFAULT '',"
			  +"`infoNum` bigint(11) NOT NULL,"
			  +"KEY `time` (`time`)"
			  +") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	}
}
